Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		scrollHeight: '',
		urlAddress1: '../../image/',
		goodsList: {
			num: 0
		},
		shopInfo: {},
		goodsType: '',
		isShowShop: false,
		isShowImg: false,
		modalFlag: true,
		number: 1,
		scrollTop: 0,
		movies: [{}],
		bannerSrc: "",
		isOneImg: false
	},
	onLoad: function (options) {
		this.getShops();
		// 生命周期函数--监听页面加载
		var _self = this;
		wx.getSystemInfo({
			success: function (res) {
				_self.setData({
					scrollHeight: (750 / res.windowWidth * res.windowHeight - 136 - 96) + 'rpx'
				})
			}
		})
		//	this.addCar();
	},
	changeDetail: function () {
		this.setData({
			modalFlag: !this.data.modalFlag
		})
		//console.log(this.data.modalFlag)
	},
	onShow: function () {

		
        this.setData({
			shopName:getApp().globalData.common.reserveShopName
		})


		wx.showShareMenu({
			withShareTicket: true,
			success() {}
		})
		if (wx.getStorageSync('openId')) {
			console.log("有openID")

			if (!getApp().globalData.userInfo.memId) {
				console.log("没有memId");
				this.login();
			}
			
		
		} else {
			console.log("没有openID");
		}
		
			if (getApp().globalData.common.reserveShopCode) {} else {
				getApp().globalData.common.reserveShopCode = getApp().globalData.common.shopCode;
			}
			this.getBanner(1);
			this.getBanner(5);
			this.initShop();
			this.getShopInfo();
			//扫码时商品存在，直接加入购物车	
			if (getApp().globalData.common.categoryCode && getApp().globalData.common.goodsCode) {
				this.addCar();
			}
	},
	/**
	 * 用户点击右上角分享（index.js）
	 */
	login() {
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/login.do',
			method: 'get',
			data: {
				openId: wx.getStorageSync('openId'),
			},
			success: function (res) {
				if (res.data.code == 200) {
					console.log("后台转前台999")
					getApp().globalData.userInfo.memId = res.data.obj.memId;
					getApp().globalData.userInfo.nickName = res.data.obj.nickName;
					getApp().globalData.userInfo.avatarUrl = res.data.obj.avatarUrl;
					getApp().globalData.userInfo.gender = res.data.obj.gender;
					getApp().globalData.userInfo.level = res.data.obj.level;
					getApp().globalData.userInfo.isBind = res.data.obj.isBind;
					getApp().globalData.header.Cookie = 'SESSION=' + res.data.obj.session;
					console.log(getApp().globalData.userInfo.memId)
				} else {
					console.log("出错");
					console.log(res.data.des);
				}
			}
		})
	},
	onShareAppMessage: function (ops) {
		if (ops.from === 'button') {
			// 来自页面内转发按钮
			console.log(ops.target)
		}
		return {
			title: '小麦铺',
			path: 'pages/loading/loading', // 路径，传递参数到指定页面。
			imageUrl: '../../image/yu_e.png', // 分享的封面图
			success: function (res) {
				// 转发成功
				console.log("转发成功:" + JSON.stringify(res));
			},
			fail: function (res) {
				// 转发失败
				console.log("转发失败:" + JSON.stringify(res));
			}
		}
	},
	//关闭页面广告图片
	closeLayer() {
		this.setData({
			isShowImg: false
		})
	},
	getBanner: function (type) {
		if (type == 0) {
			console.log('请求大图成功')
		}
		var _self = this;
		//console.log(getApp().globalData.common.reserveShopCode);
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/banner/getBanner.do?bannerType=' + type,
			data: {
				shopCode: getApp().globalData.common.reserveShopCode || '',
			},
			header: getApp().globalData.header,
			success: function (res) {
				if (res.data.code == 200) {
					if (type == 1) {
						_self.setData({
							movies: res.data.obj
						})
					} else if (type == 2) {
						_self.setData({
							btUrl: res.data.obj
						})
					} else if (type == 0) {
						_self.setData({
							movies: res.data.obj
						})
					} else if (type == 5) {
						if (res.data.obj.length > 0) {
							_self.setData({
								bannerSrc: res.data.obj[0].image,
								isShowImg: true,
								isOneImg: true
							})
							getApp().globalData.common.isOne = true;
						} else {
							_self.setData({
								isShowImg: false,
								isOneImg: false
							})
							getApp().globalData.common.isOne = false;
						}

					}
				} else {

				}
			}
		})

	},
	getShopInfo: function () {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/getShopSettings.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
				type: 2
			},
			header: getApp().globalData.header,
			success: function (res) {
				wx.hideLoading()
				if (res.data.code != 200) {
					return
				}
				_self.setData({
					distributionStartTime: res.data.obj.selfDistributionStartTime,
					distributionEndTime: res.data.obj.selfDistributionEndTime
				})
			}
		})

	},
	initShop: function () {
		if (!getApp().globalData.common.reserveShopCode) {
			this.setData({
				isShowShop: false,
				isShowImg: false,
				noteMsg: '商家歇业',
				shopInfo: {
					shopName: '请选择门店'
				}

			})
			return;
		}
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/shop/init.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
				type: 2
			},
			header: getApp().globalData.header,
			success: function (res) {
				wx.hideLoading()
				if (res.data.code == 200) {

					if (res.data.obj.status == 1) {
						_self.setData({
							isShowShop: true,
							noteMsg: '商品售罄'
						})
						//广告图，仅有第一次进入程序时展示
						//console.log(getApp().globalData.common.isOne+"______");
						if (_self.data.isOneImg) {
							if (getApp().globalData.common.isOne) {
								//console.log("+++++++++++++++++++"+666);
								_self.setData({
									isShowImg: true,
								})
								getApp().globalData.common.isOne = false;
							} else {
								_self.setData({
									isShowImg: false
								})
							}
						} else {
							_self.setData({
								isShowImg: false
							})
						}

					} else {
						//getApp().globalData.common.reserveShopCode = null;
						_self.setData({
							isShowShop: false,
							noteMsg: '商家歇业'
						})
					}
					if (res.data.obj.categoryList == null) {
						res.data.obj.categoryList = [{
							reserveCart: [],
							categoryCode: []
						}]
						_self.setData({
							isShowShop: false
						})
					}

					if (getApp().globalData.common.categoryCode) {
						//console.log(11111111);
						_self.goodsRequest(getApp().globalData.common.categoryCode);
					} else {
						//console.log(22222);
						getApp().globalData.common.categoryCode = res.data.obj.categoryList[0].categoryCode;
						//console.log(getApp().globalData.common.categoryCode);
						_self.goodsRequest(res.data.obj.categoryList[0].categoryCode);
					}

					if (getApp().globalData.common.categoryCode) {
						for (var i = 0; i < res.data.obj.categoryList.length; i++) {
							//console.log(11111111);
							//console.log(res.data.obj.categoryList[i].categoryCode);
							if (res.data.obj.categoryList[i].categoryCode == getApp().globalData.common.categoryCode) {
								//console.log(11111111);
								res.data.obj.categoryList[i].bg_style = 'nomal';
							} else {
								res.data.obj.categoryList[i].bg_style = 'unnomal';
							}
						}
					} else {
						for (var i = 0; i < res.data.obj.categoryList.length; i++) {
							if (i == 0) {
								res.data.obj.categoryList[i].bg_style = 'nomal';
							} else {
								res.data.obj.categoryList[i].bg_style = 'unnomal';
							}
						}
					}
					_self.setData({
						shopInfo: res.data.obj,
						goodsList: res.data.obj.categoryList[0].reserveCart,
						goodsType: res.data.obj.categoryList[0].categoryCode,
					})

				} else {
					_self.setData({
						isShowShop: false,
						noteMsg: '商家歇业',
						shopInfo: {
							shopName: '请选择门店'
						}
					})

				}
			}
		})
	},
	getGoodsBytype: function (e) {
		var shopInfo = this.data.shopInfo;
		getApp().globalData.common.categoryCode = e.currentTarget.dataset.code;
		//console.log(shopInfo.categoryList);
		for (var i = 0; i < shopInfo.categoryList.length; i++) {
			if (e.currentTarget.dataset.code == shopInfo.categoryList[i].categoryCode) {
				shopInfo.categoryList[i].bg_style = 'nomal';
			} else {
				shopInfo.categoryList[i].bg_style = 'unnomal';
			}
			this.setData({
				shopInfo: shopInfo,
				goodsType: e.currentTarget.dataset.code
			})
		}
		//console.log(getApp().globalData.common.categoryCode);
		this.goodsRequest(e.currentTarget.dataset.code);
	},
	goodsRequest(code) {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/goods.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
				categoryCode: code,
				type: 2

			},
			header: getApp().globalData.header,
			success: function (res) {
				//console.log(res)

				wx.hideLoading();
				if (res.data.code == 200) {
					_self.setData({
						scrollTop: 0,
						goodsList: res.data.obj
					})
				} else {

				}
			}
		})
	},
	rotateAndScaleThenTranslate: function (top, left) {
		this.setData({
			ani: 'transition: transform 1500ms ease 0ms; transform: translate(' + top + 'px, ' + left + 'px) scale(0.1, 0.1) rotate(0deg); transform-origin: 50% 50% 0px;'
		})
	},
	animationAct: function (elem, img) {
		if (!img) {
			img = this.data.urlAddress + '/xiaomai.jpg'
		}
		this.setData({
			ani: '',
			img: '',
			showAnImg: false

		})
		var uid = '#id_' + elem;
		//console.log(uid)
		var _self = this;

		var endTop;
		var endLeft;
		wx.createSelectorQuery().select('#endMarker').boundingClientRect(function (rect) {
			endTop = rect.top;
			endLeft = rect.left;
		}).exec()

		wx.createSelectorQuery().select(uid).boundingClientRect(function (rect) {

			_self.setData({
				showAnImg: true,
				topStyle: rect.top,
				leftStyle: rect.left,
				img: img
			})
			_self.rotateAndScaleThenTranslate(endLeft - rect.left, endTop - rect.top);
		}).exec()

	},
	addAction: function (e) {

		if (e.currentTarget.dataset.num == 1) {
			this.animationAct(e.currentTarget.dataset.code, e.currentTarget.dataset.img)
		}
		if (e.currentTarget.dataset.count == 0) {

		}
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})

		//请求加入购物车
		console.log(_self.data.goodsType);
		if (getApp().globalData.userInfo.memId) {
			_self.addCarRequest(e.currentTarget.dataset.code,
				getApp().globalData.common.reserveShopCode,
				e.currentTarget.dataset.num,
				getApp().globalData.common.categoryCode, 1);
		} else {
			wx.switchTab({
				url: "../../pages/mine/mine"
			});
			// wx.showToast({
			// 	title: '请先登录',
			// 	icon: 'success',
			// 	duration: 500000,
			// 	success() {
			// 		//console.log(5555);
			// 		wx.switchTab({
			// 			url: "../../pages/mine/mine"
			// 		});
			// 	}
			// })

		}

		//1 加入购物车 2默认加入购物车
	},
	turnPayPage: function () {

		
		// if (wx.getStorageSync('falg')) {
		// 	wx.showModal({
		// 	  title: '提示',
		// 	  content: '请先成为会员才能购买',
		// 	  success (res) {
		// 	    if (res.confirm) {
		// 	    	 wx.navigateTo({
		// 	            url: '../login/login'
		// 	        })
		// 	      console.log('用户点击确定')
		// 	    } else if (res.cancel) {
		// 	      console.log('用户点击取消')
		// 	    }
		// 	  }
		// 	})
		// 	return
		// }
		getApp().globalData.common.reserveVid = null;
		wx.navigateTo({
			url: '../reservePay/reservePay?type=2'
		})
	},
	//跳转购物车
	goReserveCar() {
		wx.navigateTo({
			url: '../reserveCar/reserveCar?type=' + this.data.goodsType + "&cartPrice=" + this.data.goodsList.cartPrice + "&cartDiscountAmount=" + this.data.goodsList.cartDiscountAmount
		})
	},
	//默认加入购物车功能
	addCar() {
		var _self = this;
		//商家是否营业
		if (!getApp().globalData.common.reserveShopCode) {
			_self.setData({
				isShowShop: false,
				noteMsg: '商家歇业',
				shopInfo: {
					shopName: '请选择门店'
				}

			})
			return;
		}
		_self.addCarRequest(getApp().globalData.common.goodsCode,
			getApp().globalData.common.reserveShopCode,
			_self.data.number,
			getApp().globalData.common.categoryCode, 2);
	},
	//请求加入购物车
	addCarRequest(gbk, shopCode, num, categoryCode, flag) {
		let _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/cart/add.do',
			data: {
				gbk: gbk,
				shopCode: shopCode,
				num: num,
				categoryCode: categoryCode,
				type: 2
			},
			header: getApp().globalData.header,
			success: function (res) {

				switch (flag) {
					case 1:
						wx.hideLoading();
						_self.setData({
							goodsList: res.data.obj
						});
						break;
					case 2:
						getApp().globalData.common.categoryCode = null;
						if (res.data.code != 200 || res.data.obj.num == 0) {

							wx.showToast({
								title: '商品已下架,请选择其他商品',
								icon: 'none',
								duration: 2000
							})
							return;
						}

						wx.navigateTo({
							url: '../reservePay/reservePay?type=2'
						})
						break;
				}
			}
		})
	},
	getShops: function () {
		wx.showLoading({
			title: '加载中',
			mask: true
		});
		var _self = this;
		console.log("memId------" + getApp().globalData.userInfo.memId);
		wx.getLocation({
			type: 'gcj02',
			success: function (res) {
				var latitude = res.latitude
				var longitude = res.longitude
				var speed = res.speed
				var accuracy = res.accuracy
				console.log("________" + getApp().globalData.userInfo.memId);
				wx.request({
					url: getApp().globalData.baseUrl + '/applet/reserve/shops.do',
					data: {
						x: longitude,
						y: latitude,
						memId: getApp().globalData.userInfo.memId
					},
					header: getApp().globalData.header,
					success: function (res) {
						wx.hideLoading();
						//isShowShop
						if (getApp().globalData.userInfo.memId) {
							if (res.data.obj) {
								for (let i = 0; i < res.data.obj.length; i++) {
									if (res.data.obj[i].lastConsume) {
										getApp().globalData.common.reserveShopCode = res.data.obj[i].shopCode;
										_self.setData({
											isShowShop: !_self.data.isShowShop
										})
										_self.initShop();
										_self.getShopInfo();
									}
								}
								// console.log(_self.data.isShowShop);
								_self.setData({
									shopList: res.data.obj,
								})
							}
						} else {
							wx.navigateTo({
								url: "/pages/reserveShops/reserveShops"
							})
						}

					}
				})
			},
			fail: function () {
				wx.hideLoading();
				console.log("失败")
				wx.request({
					url: getApp().globalData.baseUrl + '/applet/reserve/shops.do',
					header: getApp().globalData.header,
					success: function (res) {
						wx.hideLoading();
						_self.setData({
							shopList: res.data.obj

						})
					}
				})
			}
		})

	},
	chooseAction: function (e) {

		if (e.currentTarget.dataset.type != 1) {
			wx.showToast({
				title: '门店已歇业',
				icon: 'none',
				duration: 2000,
				mask: true,
				'success': function () {

				}
			})
			return;
		}
		getApp().globalData.common.reservebAdd = null;
		getApp().globalData.common.reservesAdd = null;
		getApp().globalData.common.reserveVid = null;
		getApp().globalData.common.reservesNote = null;
		//console.log(e.currentTarget.dataset.code)
		getApp().globalData.common.reserveShopCode = e.currentTarget.dataset.code;

		// wx.navigateBack({
		// 	delta: 1
		// })
		this.onShow();
	}

})